package com.lvyq.myplayer.widget.common.song

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
 import com.lvyq.myplayer.R
import com.lvyq.myplayer.common.util.StartActivityUtils
import com.lvyq.myplayer.database.PlayerDatabase
import com.lvyq.myplayer.model.collect.PlayCollectEntity
import com.lvyq.myplayer.parcelable.SongInfoData
import com.squareup.picasso.Picasso
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.jetbrains.anko.runOnUiThread

/**
 * 文件描述：SongItemOperationView  带操作的单曲item视图
 * 作者：lvyq
 * 创建时间：2025-03-05
 * 修改时间：2025-03-05
 */
class SongItemOperationView:RelativeLayout {
    private val playCollectDao by lazy { PlayerDatabase.getDatabase(context).playCollectDao() }

    lateinit var song :TextView
    lateinit var singer :TextView
    lateinit var timer :TextView
    lateinit var img:ImageView
    lateinit var isCollect:ImageView
    lateinit var addPlayList:ImageView

    var onAddPlayListClickListener: ((SongInfoData) -> Unit)? = null
    private var currentData: SongInfoData? = null

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    init {
        var view = View.inflate(context, R.layout.item_common_song_img, this)
        song= view.findViewById<TextView>(R.id.song)
        singer=view.findViewById<TextView>(R.id.singer)
        img = view.findViewById<ImageView>(R.id.img)
        addPlayList = view.findViewById(R.id.addPlayList)
       // isCollect=view.findViewById(R.id.isCollect)

        //通过回调外部处理收藏操作
        /*isCollect.setOnClickListener {
            currentData?.let { data ->
                val isCollected = MyCollectSharePrefsManager.isCollectDataExists(context, data.id)
                onCollectClickListener?.invoke(data, !isCollected)
            }
        }*/


    }
    @SuppressLint("SuspiciousIndentation")
    fun setData(data: SongInfoData) {
        this.currentData = data

        song.setText(data.title)
        singer.setText(data.singer)
        Picasso.with(context).load(data.bgImg).into(img)

        //添加到下一首
        addPlayList.setOnClickListener {
            onAddPlayListClickListener?.invoke(data)
        }


        /*     //TODO 收藏状态
             var isCollected=false
             CoroutineScope(Dispatchers.IO).launch {
                 val collectDataExists = playCollectDao.getPlayCollectByBusinessKeyAndType(data.id,1)
                     context.runOnUiThread {
                         isCollect.setImageResource(
                             if (collectDataExists!=null){
                                 isCollected=true
                                 R.mipmap.audio_collect
                             } else{
                                 R.mipmap.pop_play_shoucang
                             }
                         )
                 }
             }*/

        // 内部处理，
     /*   isCollect.setOnClickListener {
            currentData?.let {
                toggleCollectState(it)
                //回调通知，用于在收藏页取消收藏时，刷新列表使用
                onCollectClickListener?.invoke(data, !isCollected)
            }
        }*/
    }

    private fun toggleCollectState(data: SongInfoData) {
        CoroutineScope(Dispatchers.IO).launch {
            val collectDataExists = playCollectDao.getPlayCollectByBusinessKeyAndType(data.id,1)
            if (collectDataExists!=null) {
                // 如果已收藏，则取消收藏
                playCollectDao.deleteByBusinessKey(data.id, 1)

                context.runOnUiThread {
                    isCollect.setImageResource(R.mipmap.pop_play_shoucang)
                    Toast.makeText(this, "取消收藏", Toast.LENGTH_SHORT).show()
                }
            } else {
                // 如果未收藏，则添加收藏
                val currentTimeStamp = System.currentTimeMillis()
                val newEntry = PlayCollectEntity(title=data.title?:"",
                    businessKey = data.id,
                    singer = data.singer,
                    bgImg = data.bgImg?:"",
                    url = data.song,
                    lrc = data.lrc,
                    singerId = data.singerId,
                    publishDate = data.publishDate,
                    albumId = data.albumId,
                    albumName = data.albumName,
                    createTime = currentTimeStamp,
                    updateTime = currentTimeStamp,
                    totalDuration = 0,
                    playCount = 1,
                    type = 1)
               playCollectDao.insertPlayCollect(newEntry)

                context.runOnUiThread {
                    isCollect.setImageResource(R.mipmap.audio_collect)
                    Toast.makeText(this, "收藏成功", Toast.LENGTH_SHORT).show()
                }
            }
        }

    }

}